package com.novel.common.init;

import com.novel.common.pojo.RedisParamKey;
import com.novel.mapper.NovelCategoryMapper;
import com.novel.mapper.NovelMapper;
import com.novel.mapper.RechargeMapper;
import com.novel.pojo.novel.Novel;
import com.novel.pojo.novel.NovelCategory;
import com.novel.service.RechargeService;
import com.novel.utils.GsonTool;
import com.novel.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 服务启动完成之后立马执行
 * 把小说信息同步到redis中
 */
@Order(1)
@Component
public class InitSystemConfig implements ApplicationRunner {

    @Autowired
    private NovelMapper novelMapper;

    @Autowired
    private NovelCategoryMapper novelCategoryMapper;

    @Autowired
    private RechargeService rechargeService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 同步小说中的各种信息
     * 1.小说分类信息
     * 2.小说书籍基本信息
     * 3.在线人数
     * 4.小说点击量
     */
    @Override
    public void run(ApplicationArguments args) {
        //设置在线人数
        redisUtils.stringSet(RedisParamKey.onlineCount, 0 + "");
        //查询小说分类信息
        List<NovelCategory> categoryList = novelCategoryMapper.selectAll();
        redisUtils.stringSet(RedisParamKey.novelCategory, GsonTool.toJson(categoryList));
        //查询书籍基本信息
        List<Novel> novelList = novelMapper.selectAll();
        novelList.forEach(novel -> {
            //设置小说信息
            redisUtils.hashSet(novel.getNovelId(), RedisParamKey.novelInfo, GsonTool.toJson(novel));
            //设置小说点击量
            redisUtils.hashSet(novel.getNovelId(), RedisParamKey.novelClick, novel.getNovelClick() + "");
        });

        //设置充值数额
        redisUtils.stringSet(RedisParamKey.rechargeList, GsonTool.toJson(rechargeService.getRechargeList()));
    }
}